package com.campus_information_portal.controller;/**
 * @Author:20155887赵开兰
 * @Description:
 * @Date: Created in 2018/5/24 9:59
 */

import com.campus_information_portal.entity.DorStuTerm;
import com.campus_information_portal.entity.Party;
import com.campus_information_portal.entity.PartyMember;
import com.campus_information_portal.entity.StuApply;
import com.campus_information_portal.service.DormitoryService;
import com.campus_information_portal.service.PartyApplyService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: campus_information_portal
 * @description: 学生申请入党和老师处理申请控制层
 * @author: 20155887赵开兰
 * @create: 2018-05-24 09:59
 **/
@Controller
@RequestMapping("/partyApply")
@ResponseBody
@CrossOrigin(origins = "*")
public class PartyApplyController {
    @Autowired
    private PartyApplyService partyApplyService;

    /**
     * @Description :学生申请入党成功方法，绝对正确
     * @author 20155887 赵开兰
     * @methodName
     * @param: HttpServletRequest request, HttpServletResponse response
     * @returnType
     * @create 2018/5/25 16:03
     **/

    @RequestMapping(value = "/applyParty2", method = RequestMethod.POST)
    public void getMsg(HttpServletRequest request, HttpServletResponse response) {
        String stuNum = request.getParameter("stuNum");
        String stuName = request.getParameter("stuName");
        String partyNum = request.getParameter("partyNum");
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        System.out.println("学生学号为：" + stuNum + "学生姓名为：" + stuName + "党组织编号为："
                + partyNum + "申请日期为：" + sdf.format(nowDate));
        //通过学生学号查询学生ID
        int stuId = partyApplyService.queryStuIdByStuNum(Integer.parseInt(stuNum));
        System.out.println(stuId + "---");
        StuApply stuApply = new StuApply(stuId, Integer.parseInt(stuNum), stuName, new Date(), Integer.parseInt(partyNum), 0);
        System.out.println("---学生申请入党------");
        partyApplyService.addApplyParty(stuApply);
        System.out.println("---学生申请入党和老师处理申请控制层已执行---");
        //学生提交入党申请成功后政治面貌变为积极分子
        /*  partyApplyService.updateStudentPoli(Integer.parseInt(stuNum));*/

    }

    /**
     * @param
     * @Description :前台Ajax动态获取所有党组织
     * @author 20155887 赵开兰
     * @methodName ：getPartys
     * @returnType ： List<Party>
     * @create 2018/5/26 20:17
     **/

    @RequestMapping(value = "/queryPartys", method = RequestMethod.GET)
    public List<Party> getPartys() {
        //  System.out.println("进来了吗？？？？？？？？？？？？？？？");
        List<Party> partyList = new ArrayList<Party>();
        partyList = partyApplyService.queryAllPartys();
        // partyList.forEach(party -> System.out.println(party.toString()));
        return partyList;
    }

    /**
     * @param
     * @Description :老师无条件查询所有学生的申请入党记录
     * @author 20155887 赵开兰
     * @methodName
     * @returnType
     * @create 2018/5/27 0:39
     **/

    @RequestMapping(value = "/queryAllStuApplys", method = RequestMethod.GET)
    public Message queryAllStuApplys() {
        return partyApplyService.queryAllStuApplys();
    }

    /**
     * @param: 未解决根据姓名查询的中文乱码问题
     * @Description: 老师多条件动态查询所有学生的申请入党记录
     * @author 20155887 赵开兰
     * @methodName: queryMyDormitoryApplyForms
     * @returnType: Message
     * @create 2018/5/27 11:09
     **/
    @RequestMapping(value = "/querystuApplyConditions", method = RequestMethod.GET)
    public Message queryMyDormitoryApplyForms(StuApply stuApply) {
        System.out.println(stuApply.toString() + "------");
        System.out.println(partyApplyService.queryApplyByConditions(stuApply));
        return partyApplyService.queryApplyByConditions(stuApply);
    }

    /**
     * @param
     * @Description :老师同意学生申请
     * @author 20155887 赵开兰
     * @methodName
     * @returnType
     * @create 2018/5/27 13:41
     **/

    @RequestMapping(value = "/agreeStuApply", method = RequestMethod.GET)
    public Message agreeStuApply(@Param("stuApplyId") Integer stuApplyId) {
        // System.out.println("---stuApplyId:" + stuApplyId);
        StuApply stuApply = (StuApply) partyApplyService.querystuApplyBystuApplyId(stuApplyId)
                .getData().get("stuApply");
        //  System.out.println("同意----@@" + stuApply.toString());
        partyApplyService.replyOfStuApplyPartyYes(stuApplyId);
        //  System.out.println("----学生学号为：" + stuApply.getStuNum());
        //通过党组织num查询党组织ID:party_info_id
        // System.out.println("---欲申请的党组织Num为:" + stuApply.getPartyOrganizationNum());
        int partyNum = stuApply.getPartyOrganizationNum();
        int PartyInfoId = partyApplyService.queryPartyIdByPartyNum(partyNum);
        //  System.out.println("---党组织Id为:" + PartyInfoId);
        //通过学生学号查询学生id
        int StuInfoId = partyApplyService.queryStuIdByStuNum(stuApply.getStuNum());
        //  System.out.println("StuInfoId:" + StuInfoId);
        PartyMember partyMember = new PartyMember();
        partyMember.setPartyInfoId(PartyInfoId);
        partyMember.setTeInfoId(0);
        partyMember.setStuInfoId(StuInfoId);
        partyMember.setPartyMemberAccount(stuApply.getStuNum());
        partyMember.setPartyMemberName(stuApply.getStuName());
        partyMember.setPartyMemberType(1);//设置初始状态1，代表积极分子
        partyMember.setPartyOrganizationNum(stuApply.getPartyOrganizationNum());
        partyMember.setApplyDate(stuApply.getApplyDate());
        //同意入党的同时向党员表添加一条记录
        partyApplyService.addPartyMember(partyMember);
        //更改学生政治面貌为积极分子：state=1
        partyApplyService.updateStudentPoli(stuApply.getStuNum());
        return partyApplyService.replyOfStuApplyPartyYes(stuApplyId);

    }

    /**
     * @param: Integer stuApplyId
     * @Description: 老师拒绝学生入党申请
     * @author 20155887 赵开兰
     * @methodName: refusePartyApply
     * @returnType: Message
     * @create 2018/5/27 23:39
     **/
    @RequestMapping(value = "/refuseApplyParty", method = RequestMethod.GET)
    public Message refusePartyApply(@Param("stuApplyId") Integer stuApplyId) {
        System.out.println("已进入拒绝学生入党");
        return partyApplyService.replyOfStuApplyPartyDeny(stuApplyId);
    }

    /**
     * @param
     * @Description :学生查询个人党组织情况和个人入党情况
     * @author 20155887 赵开兰
     * @methodName ：queryMyParty
     * @returnType
     * @create 2018/5/28 15:13
     **/

    @RequestMapping(value = "/queryMyParty", method = RequestMethod.GET)
    public Message queryMyParty(@Param("partyMemberAccount") int partyMemberAccount) {
        //通过党员账号即学生学号获取到个人所在党组织
        Party myParty = partyApplyService.queryPartyByPartyMemberAccount(partyMemberAccount);
        //通过党员账号即学生学号获取到个人党员信息
        PartyMember myPartyMember = partyApplyService.querypartyMemberByAccount(partyMemberAccount);
        Message message = new Message();
        message.add("MyParty", myParty);
        message.add("PartyMemberMe", myPartyMember);
        return message;
    }
}
